import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:ball_lion/server/api/record_api.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/bottom_picker_widget.dart';

class RankLogic extends GetxController {
  String tag = "";
  int _sexIndex = 0;
  int _typeIndex = 0;
  int _dateIndex = 0;
  List<String> sexList = ["男足", "女足"];
  List<String> sexBbList = ["男篮", "女篮"];
  List<String> typeList = ["世界", "亚洲", "欧洲", "南美洲", "中北美洲", "大洋洲", "非洲"];
  List<String> typeBbList = ["世界", "亚洲", "欧洲", "美洲", "非洲"];
  List<int> type = [0, 4, 2, 3, 6];
  List? _months;
  List? _data;
  EasyRefreshController refreshController = EasyRefreshController();

  bool isLoading = true;
  int get sexIndex => _sexIndex;
  int get dateIndex => _dateIndex;
  int get typeIndex => _typeIndex;
  List? get data => _data;
  List? get months => _months;

  Future getData() async {
    _data = [];
    if (tag != "篮球") {
      _months = await RecordApi.getZqRankMonth(tag == "俱乐部" ? 3 : sexIndex + 1);
      _data = await RecordApi.getZqRank(
          type: tag == "俱乐部" ? 3 : sexIndex + 1,
          area: tag == "俱乐部" || _typeIndex == 0 ? "" : typeList[_typeIndex],
          month: _months?[_dateIndex].rankMonth);
    } else {
      _months = await RecordApi.getLqRankMonth(sexIndex + 1);
      _data = await RecordApi.getLqRank(
          type: sexIndex + 1,
          area: type[_typeIndex],
          month: _months?[_dateIndex].rankMonth);
    }
    isLoading = false;
    update();
  }

  Future showTypePicker() async {
    _sexIndex = await Get.bottomSheet(BottomPickerWidget(
      tag != "篮球" ? sexList : sexBbList,
      title: '选择类型',
      initialIndex: _sexIndex,
    ));
    await getData();
    update();
  }

  Future showDatePicker() async {
    _dateIndex = await Get.bottomSheet(BottomPickerWidget(
      _months?.map((e) => e.rankMonth! as String).toList() ?? [],
      title: '选择月份',
      initialIndex: _dateIndex,
    ));
    await getData();
    update();
  }

  void setType(index) {
    _typeIndex = index;
    getData();
    update();
  }

  void doRefresh() {
    if (refreshController.headerState?.mode.isIdle == false) return;
    refreshController.callRefresh();
  }
}
